#include <bits/stdc++.h>

# define INF 0x7fffffff;  // 无穷大

using namespace std;

typedef long long ll;

class Solution {
public:
    long long minEnd(int n, int x) {
        ll ans = x;
        n--;
        int xBitIdx = 0;
        while (n > 0) {
            ll d = n & 1;
            n >>= 1;
            while(ans & (1LL << xBitIdx)) xBitIdx++;
            ans |= (d << xBitIdx);
            xBitIdx++;
        }
        return ans;
    }
};

int main(int argc, const char** argv) {
    Solution s;
    auto res = s.minEnd(6715154, 7193485);
    std::cout << res << std::endl;
    return 0;
}
